package logs

import (
	log "github.com/sirupsen/logrus"
	"time"
)

var (
	g_local_addr = ""
	g_qps_data   = make(map[string]*QPSData)
)

type QueryLog struct {
	Host   string `json:"host"`
	Domain string `json:"domain"`

	QueryType    string `json:"query_type"`
	SourceAddr   string `json:"source_addr"`
	EdnsAddr     string `json:"edns_addr,omitempty"`
	Edge         string `json:"line_code,omitempty"`
	Answer       string `json:"answer,omitempty"`
	QueryMachine string `json:"query_machine,omitempty"`

	Published time.Time `json:"timestamp"`

	//2025.2.28 add log type
	LogType string `json:"log_type"`
}

type QPSData struct {
	Host   string `json:"host"`
	Domain string `json:"domain"`
	//SourceAddr string `json:"source_addr,omitempty"`
	Total int32 `json:"total"`
	Tcp   int32 `json:"tcp"`
	Udp   int32 `json:"udp"`

	Published time.Time `json:"timestamp"`
}

type HttpDNSData struct {
	Host   string `json:"host"`
	Domain string `json:"domain"`

	Total     int32  `json:"total"`
	Https     int32  `json:"https"`
	Aes       int32  `json:"aes"`
	Des       int32  `json:"des"`
	QueryType string `json:"query_type"`

	Published time.Time `json:"timestamp"`
}

func InitData() {
	var err error
	//var data = make([]interface{}, 0)
	//
	////insert some test data
	//for i := 0; i < 990*1000; i++ {
	//	data = append(data, &QPSData{
	//		Host:      "121.62.17.151",
	//		Domain:    "www.baidu.com",
	//		Total:     100,
	//		Tcp:       50,
	//		Udp:       i,
	//		Published: time.Now().Round(time.Second).Add(time.Duration(i) * time.Second),
	//	})
	//}
	//
	//err := SendDataToEs("qps", data)
	//if err != nil {
	//	log.Error(err)
	//}
	//
	//var data2 = make([]interface{}, 0)
	////insert some test data
	//for i := 0; i < 90*1000; i++ {
	//
	//	data2 = append(data2, &QueryLog{
	//		Host:       "121.62.17.151",
	//		Domain:     "www.wildbee.cn",
	//		QueryType:  "A",
	//		SourceAddr: "1.1.1.1",
	//		Published:  time.Now().Round(time.Second).Add(time.Duration(i) * time.Second),
	//	})
	//}
	//err = SendDataToEs("query-log", data2)
	//if err != nil {
	//	log.Error(err)
	//}

	//insert some test data for http dns
	var data3 = make([]interface{}, 0)
	for i := 0; i < 10000; i++ {
		data3 = append(data3, &HttpDNSData{
			Host:      "121.62.17.151",
			Domain:    "www.wildbee.cn",
			QueryType: "AAAA",
			Total:     100,
			Https:     50,
			Aes:       int32(i),
			Des:       int32(i),
			Published: time.Now().Round(time.Second).Add(time.Duration(i) * time.Second),
		})
	}
	err = SendDataToEs("http-dns", data3)
	if err != nil {
		log.Error(err)
	}
}

//func AddQpsData(domain string, is_tcp bool) {
//	func() {
//		if _, ok := g_qps_data[domain]; !ok {
//			g_qps_data[domain] = &QPSData{
//				Host:      g_local_addr,
//				Domain:    domain,
//				Total:     0,
//				Tcp:       0,
//				Udp:       0,
//				Published: time.Now().Round(time.Second),
//			}
//		}
//	}()
//}
